package com.example.teesystem.controller;

import com.example.teesystem.common.exception.CustomException;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.vo.dailyIssue.DailyIssueSubVo;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.service.IssueService;
import com.example.teesystem.service.WrongSetService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping(value = "/tee/dailyIssue", produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
@Validated
public class DailyIssueController {
    private final WrongSetService wrongSetService;
    private final IssueService issueService;
    
    /**
     * 根据学生 id 获取每日一题
     * @return
     */
    @PostMapping("/get")
    public Response getIssue(@RequestParam("courseId") Integer courseId) {
        return Response.ok(wrongSetService.get(MyHandlerInterceptor.getUid(), courseId));
    }
    
    /**
     * 提交答案
     * @param subVo 封装提交信息
     * @return
     */
    @PostMapping("/sub")
    public Response subIssue(@Valid @RequestBody DailyIssueSubVo subVo) {
        if(wrongSetService.sub(subVo)) {
            return Response.ok();
        } else {
            throw new CustomException();
        }
    }

    /**
     * 趣味训练
     * @param map
     * @return
     */
    @PostMapping(value = "/interestTraining")
    public Response getInterestTraining(@RequestBody Map<String ,String> map){
        int courseId,difficulty,type,chapterId;
        try{
            courseId = Integer.parseInt(map.get("courseId"));
            difficulty = Integer.parseInt(map.getOrDefault("difficulty","-1"));
            type = Integer.parseInt(map.getOrDefault("type","-1"));
            chapterId = Integer.parseInt(map.getOrDefault("chapterId","-1"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        if(difficulty!=1){
            if(difficulty>5||difficulty<1) {
                return Response.error(ResultCode.PARAMETER_ERROR);
            }
        }
        if(type!=-1){
           if(type!=0&&type!=2&&type!=3) {
               return Response.error(ResultCode.PARAMETER_ERROR);
           }
        }
        return issueService.getInterestIssue(courseId,difficulty,type,chapterId);
    }
    /**
     *
     */

    /**
     * 回答训练
     * @param map
     * @return
     */
    @ApiOperation("回答训练")
    @PostMapping(value = "/answer")
    public Response answer(@RequestBody Map<String,String> map){
        int issueId;
        String answer;
        try{
            issueId = Integer.parseInt(map.get("issueId"));
            answer = map.get("answer");
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        return issueService.checkAnswer(issueId, answer);
    }
}
